<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <!-- 添加按钮 -->
      <el-row>
        <el-col>
          <el-button @click="addCate" type="primary">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table
        :data="goodsCateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
      >
        <template slot="isOk" scope="scope">
          <i
            v-if="scope.row.cat_deleted === false"
            class="el-icon-success"
            style="color: green"
          ></i>
          <i v-else class="el-icon-success" style="color: red"></i>
        </template>
        <template slot="order" scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level === 1" type="success"
            >二级</el-tag
          >
          <el-tag v-else type="warning">三级</el-tag>
        </template>
        <template slot="operate" scope="scope">
          <el-button size="mini" @click="editCate(scope.row)" type="primary" icon="el-icon-edit"
            >编辑</el-button
          >
          <el-button size="mini" @click="deleteCate(scope.row)" type="danger" icon="el-icon-delete"
            >删除</el-button
          >
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="goodsParams.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="goodsParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类的对话框 -->
    <el-dialog title="添加分类" :visible.sync="showAddCateDialog"
      @close='closeDialog' width="50%">
      <el-form
        :model="requestAddParams"
        :rules="addCateRule"
        ref="addCateRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="requestAddParams.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
          v-model="checkValue"
          :options="options"
          :props="cascaderProps"
          @change="handleChange"
          clearable
          change-on-select
        ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showAddCateDialog = false">取 消</el-button>
        <el-button type="primary" @click="addCateOk"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 弹出编辑分类对话框 -->
    <el-dialog
  title="编辑分类"
  :visible.sync="showEditDialog"
  width="50%">
  <el-form :model="editModel" :rules="addCateRule" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="分类名称" prop="cat_name">
    <el-input v-model="editModel.cat_name"></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="showEditDialog = false">取 消</el-button>
    <el-button type="primary" @click="editCateOk">修 改</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.getGoodsCateList();
  },
  data() {
    return {
      // 获取到的商品列表信息
      goodsCateList: [],
      // 获取商品列表携带的参数
      goodsParams: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      // 总的商品个数
      total: 0,
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          prop: "cat_deleted",
          type: "template",
          template: "isOk",
        },
        {
          label: "排序",
          prop: "cat_level",
          type: "template",
          template: "order",
        },
        {
          label: "操作",
          type: "template",
          template: "operate",
        },
      ],
      // 是否显示分类的对话框
      showAddCateDialog: false,
      // 添加分类的验证规则
      addCateRule: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "长度在 1 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
      // 添加分类表单绑定的数据
      // addCateForm: {
      //   cat_name: "",
      // },
      // 级联选择器选择的值
      checkValue: [],
      // 数据源
      options: [],
      cascaderProps:{
        value:'cat_id',
        label:'cat_name',
        children:'children',
        expandTrigger: 'hover',
      },
      // 请求添加携带的参数
      requestAddParams:{
        cat_pid:0,
        cat_name:'',
        cat_level:0
      },
      // 控制编辑分类弹出对话框
      showEditDialog:false,
      // 编辑分类绑定的参数
      editModel:{
        cat_name:''
      },
      // 需要修改分类名字的id
      editId:0,
      
    };
  },
  methods: {
    async getGoodsCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: this.goodsParams,
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品分类列表失败");
      } else {
        // console.log(res);

        this.goodsCateList = res.data.result;
        this.total = res.data.total;
        // console.log(this.total);
        // console.log(this.goodsCateList);
      }
    },
    // 每页有几条
    handleSizeChange(newsize) {
      this.goodsParams.pagesize = newsize;
      this.getGoodsCateList();
      // console.log(newsize);
    },
    // 当前页
    handleCurrentChange(newpage) {
      this.goodsParams.pagenum = newpage;
      // console.log(newpage);
      this.getGoodsCateList();
    },
    // 点击添加分类
    async addCate() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 },
      });
      // console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("获取所有商品分类失败");
      } else {
        this.options = res.data;
        // console.log(this.options);
      }
      this.showAddCateDialog = true;
    },
    handleChange(value) {
      // console.log(value);
      if(value.length==1){
        this.requestAddParams.cat_pid=value[0]
        this.requestAddParams.cat_level=1
      }
      else if(value.length==2){
        this.requestAddParams.cat_pid=value[1]
        this.requestAddParams.cat_level=2
      }
    },
    // 输入分类后点击确定按钮
    async addCateOk(){
      const {data:res}= await this.$http.post('categories',this.requestAddParams)
      console.log(res);
      this.requestAddParams.cat_level=0
      this.requestAddParams.cat_pid=0
      this.requestAddParams.cat_name=''
      this.getGoodsCateList()
      
      this.showAddCateDialog=false
      
    },
    closeDialog(){
      this.$refs.addCateRef.resetFields()
      this.checkValue=[]
    },
    // 编辑分类的名字
    editCate(info){
      console.log(info);
      this.editModel.cat_name=info.cat_name
      this.editId=info.cat_id
      
      // console.log(this.editCateName);
      this.showEditDialog=true
    },
    // 修改分类的名字OK
    async editCateOk(){
      // console.log(this.editModel.cat_name);
      const {data:res}=await this.$http.put('categories/'+this.editId,{cat_name:this.editModel.cat_name})

      if(res.meta.status!==200) {
        this.$message.error('修改分类名称失败')
        
      }
      else{
        this.$message.success('修改分类名称成功')
        // console.log(res);
        this.getGoodsCateList()
        this.showEditDialog=false
      }
    },
    // 删除分类
    deleteCate(id){
      // console.log(id);
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const {data:res}=await this.$http.delete('categories/'+id.cat_id)
          if(res.meta.status!==200){
            return this.$message.error('删除该分类失败')
          }
          else{
            this.$message.success('删除分类成功')
            this.getGoodsCateList()
          }
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    }
  },
};
</script>

<style scoped>
.zk-table {
  margin-top: 15px !important;
}
.el-cascader{
  width: 100%;
  
}

</style>